﻿using System.Activities;
using System.Activities.DurableInstancing;
using System.Collections.Generic;
using Reactor.Environment;

namespace Reactor.Workflow
{
    public static class WorkflowApplicationFactory
    {
        private static SqlWorkflowInstanceStore _instanceStore;

        static WorkflowApplicationFactory()
        {
            var connectionData = Context.ServiceLocator.GetInstance<PlatformConnectionData>();
            _instanceStore = new SqlWorkflowInstanceStore(connectionData.DatabaseConnectionString);
        }

        public static WorkflowApplication GetWorkflowApplication(Activity workflow)
        {
            var workflowApplication = new WorkflowApplication(workflow)
                                          {
                                              InstanceStore = _instanceStore
                                          };

            return workflowApplication;
        }

        public static WorkflowApplication GetWorkflowApplication(Activity workflow, Dictionary<string, object> arguments)
        {
            var workflowApplication = (arguments != null) 
                ? new WorkflowApplication(workflow) 
                : new WorkflowApplication(workflow, arguments);

            workflowApplication.InstanceStore = _instanceStore;

            return workflowApplication;
        }
    }
}
